
export class Rect {
    constructor(x, y, w, h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.x1 = x + w;
        this.y1 = y + h;
    }
    testIn(x, y) {
        return x >= this.x && x <= this.x1 &&
            y >= this.y && y <= this.y1;
    }
}

export class Color {
    constructor(str) {
        let r, g, b, a = 1, res;
        str = str.replace(/\s/g, '');
        if (res = str.match(/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/)) {
            r = parseInt(res[1], 16);
            g = parseInt(res[2], 16);
            b = parseInt(res[3], 16);
            r = r * 16 + r;
            g = g * 16 + g;
            b = b * 16 + b;
        } else if (res = str.match(/^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/)) {
            r = parseInt(res[1], 16);
            g = parseInt(res[2], 16);
            b = parseInt(res[3], 16);
            
        } else if (res = str.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),(\d?.?(\d+)?)\)$/i)) {
            r = parseInt(res[1], 10);
            g = parseInt(res[2], 10);
            b = parseInt(res[3], 10);
            a = parseFloat(res[4]);
        } else if (res = str.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/i)) {
            r = parseInt(res[1], 10);
            g = parseInt(res[2], 10);
            b = parseInt(res[3], 10);
        }
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
    get rgba() {
        const {r, g, b, a} = this;
        return `rgba(${r}, ${g}, ${b}, ${a})`;
    }
}
